import 'package:dio/dio.dart';
import 'package:flutter_09_game/config/app_config.dart';
import 'package:flutter_09_game/config/app_language.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:get/get.dart';

class RequestInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    _setRequestHeaders(options);
    handler.next(options);
    // logWrite("${options.uri.toString()}\n${options.data}");
  }

  @override
  Future onError(DioException err, ErrorInterceptorHandler handler) async {
    super.onError(err, handler);
    // logWrite("${err.requestOptions.uri.toString()}\n$err");
  }

  _setRequestHeaders(RequestOptions options) {
    // 基础请求头
    Map<String, dynamic> header = {
      'Authorization': AppUserManager().token,
    };

    // 添加通用请求头信息
    header.addAll({
      'accept': 'application/json',

      'x-client-platform': GetPlatform.isAndroid
          ? 'android_app'
          : GetPlatform.isIOS
              ? 'ios_app'
              : 'web_app',
      'uid': AppUserManager().uid,
      //   'X-Device-ID': AppConfig.deviceId,
      //   'X-App-Version': AppConfig.version,
      'Content-Type': 'application/json;Charset=UTF-8',
    });

    // clog('header   ${header}');
    options.headers = header;
  }
}
